package com.youlai.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.entity.ProductCategory;
import com.youlai.system.model.form.CategoryForm;
import com.youlai.system.model.query.CategoryPageQuery;
import com.youlai.system.model.vo.CategoryPageVo;
import com.youlai.system.model.vo.CategoryTreeVo;
import com.youlai.system.service.ProductCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @Author zzps
 * @Date 2024/3/11 10:10
 * @注释
 */
@Tag(name = "商品分类管理相关接口")
@RestController
@RequestMapping("/api/category")
public class ProductCategoryController {
    @Resource
    private ProductCategoryService productCategoryService;

    @Operation(summary = "分页查询商品分类列表")
    @GetMapping("/list")
    public PageResult<CategoryPageVo> list(@ParameterObject CategoryPageQuery categoryPageQuery) {
        IPage<CategoryPageVo> categoryList= productCategoryService.getCategoryPageList(categoryPageQuery);
        return PageResult.success(categoryList);
    }

    @Operation(summary = "根据id查询商品分类")
    @GetMapping("/{id}")
    public Result<CategoryPageVo> getById(@PathVariable Long id) {
        ProductCategory category = productCategoryService.getById(id);
        CategoryPageVo categoryPageVo = new CategoryPageVo();
        BeanUtils.copyProperties(category, categoryPageVo);
        return Result.success(categoryPageVo);
    }

    @Operation(summary = "新增商品分类")
    @PostMapping("/add")
    public Result<Void> add(@RequestBody @Valid CategoryForm categoryForm) {
        boolean result =  productCategoryService.addCategory(categoryForm);
        return Result.judge(result);
    }

    @Operation(summary = "修改商品分类")
    @PutMapping("/update")
    public Result<Void> update(@RequestBody @Valid CategoryForm categoryForm) {
        boolean result =  productCategoryService.updateCategory(categoryForm);
        return Result.judge(result);
    }

    @Operation(summary = "修改商品分类状态")
    @PutMapping("/update/status/{id}/{status}")
    public Result<Void> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        boolean result =  productCategoryService.updateCategoryStatus(id, status);
        return Result.judge(result);
    }

    @Operation(summary = "删除商品分类")
    @DeleteMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        boolean result =  productCategoryService.removeCategoryById(id);
        return Result.judge(result);
    }

    @Operation(summary = "查询商品分类树")
    @GetMapping("/tree")
    public Result<List<CategoryTreeVo>> tree() {
        List<CategoryTreeVo> categoryTree = productCategoryService.getCategoryTree();
        return Result.success(categoryTree);
    }

    @Operation(summary = "查询一二三级分类")
    @GetMapping("/level/{level}")
    public Result<List<CategoryPageVo>> level(@PathVariable Integer level) {
        List<CategoryPageVo> categoryList = productCategoryService.getCategoryByLevel(level);
        return Result.success(categoryList);
    }
}
